package com.zzsy.carosshopprojects.activitys.home.mainpage.orderamount;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.king.base.util.ToastUtils;
import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.base.BaseFragment;
import com.zzsy.carosshopprojects.base.ConfigureFile;
import com.zzsy.carosshopprojects.bean.MessageEvent;
import com.zzsy.carosshopprojects.bean.OrderDetialResultBean;
import com.zzsy.carosshopprojects.util.HUD;
import com.zzsy.carosshopprojects.util.StringUtils;
import com.zzsy.carosshopprojects.util.ThrottleFirstUtil;
import com.zzsy.carosshopprojects.util.ViewBgUtil;
import com.zzsy.carosshopprojects.util.sunmiprint.PrintHelper;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 订单金额详情
 */
public class OrderAmountDetailFragment extends BaseFragment {
    public static OrderAmountDetailFragment newInstance(String orderNum) {
        OrderAmountDetailFragment orderAmountDetailFragment = new OrderAmountDetailFragment();
        Bundle bundle = new Bundle();
        bundle.putString("orderNum", orderNum);
        orderAmountDetailFragment.setArguments(bundle);
        return orderAmountDetailFragment;
    }

    @BindView(R.id.tv_station_name)
    TextView tvStationName;
    @BindView(R.id.tv_oils_nums)
    TextView tvOilsNums;
    @BindView(R.id.tv_yh_amount)
    TextView tvYHAmount;
    @BindView(R.id.tv_sf_amount)
    TextView tvSFAmount;
    @BindView(R.id.tv_order_nums)
    TextView tvOrderNums;
    @BindView(R.id.tv_pay_type)
    TextView tvPayType;
    @BindView(R.id.tv_pay_time)
    TextView tvPayTime;
    @BindView(R.id.tv_fp_state)
    TextView tvFPState;
    @BindView(R.id.iv_phone_bar)
    TextView ivPrintBar;
    @BindView(R.id.tv_coupon_num)
    TextView tvCouponNum;
    @BindView(R.id.tv_all_coupon)
    TextView tvAllCoupon;
    @BindView(R.id.tv_oils_price)
    TextView tvOilsPrice;
    @BindView(R.id.tv_oils_rise)
    TextView tvOilsRise;
    @BindView(R.id.tv_tremmin_num)
    TextView tvTremNum;
    @BindView(R.id.tv_member_phone)
    TextView tvMmeberPhone;
    @BindView(R.id.tv_member_real_name)
    TextView tvMemberRealName;
    @BindView(R.id.ll_phone)
    LinearLayout llPhone;
    @BindView(R.id.ll_name)
    LinearLayout llName;
    @BindView(R.id.tv_certification_typeName)
    TextView tvCertiftionTypeName;
    @BindView(R.id.tv_car_number)
    TextView tvCarNumber;
    @BindView(R.id.ll_certification_type)
    LinearLayout llCertificationType;
    @BindView(R.id.ll_car_number)
    LinearLayout llCarNumber;
    @BindView(R.id.rl_view)
    LinearLayout rl_view;
    @BindView(R.id.tv_oils_wenan)
    TextView tvOilsWenAn;
    private OrderAmountDetailMode mode;
    private OrderDetialResultBean orderDetialResultBean;
    private String pwd = "";

    @Override
    protected int initLayout() {
        return R.layout.fg_orderamountdetail;
    }

    @Override
    protected void initView(View view) {
        initTitle(view, getResources().getString(R.string.OrderDetail), getActivity());
        mode = getViewModelProvider().get(OrderAmountDetailMode.class);
        rl_view.setBackground(ViewBgUtil.setBg(15, Color.parseColor(ConfigureFile.ColorWhite)));
    }

    @Override
    protected void initData(Context mContext) {

    }

    @Override
    protected void loadMethod() {
        getDataDetail();
        showDialog();
    }

    @OnClick({R.id.iv_phone_bar, R.id.iv_refund})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_phone_bar:
                if (orderDetialResultBean == null) {
                    getActivity().finish();
                    ToastUtils.showToast(getContext().getApplicationContext(), "订单详情异常,无法打印小票！");
                    return;
                }
                if (!StringUtils.isEmpty(orderDetialResultBean.getOrderSource())) {
                    if ("1".equals(orderDetialResultBean.getOrderSource())) {
                        printOneCareoe();
                    } else if ("3".equals(orderDetialResultBean.getOrderSource())) {
                        if (!StringUtils.isEmpty(orderDetialResultBean.getOrderNumber())) {
                            if (StringUtils.isEmpty(orderDetialResultBean.getMemberId())) {
                                ToastUtils.showToast(getContext().getApplicationContext(), "该订单会员信息异常！");
                                return;
                            } else {
                                if ("0".equals(orderDetialResultBean.getMemberId())) {
                                    printYouKe();
                                } else {
                                    print();
                                }
                            }
                        }
                    }
                } else {
                    ivPrintBar.setVisibility(View.GONE);
                }
                break;
            case R.id.iv_refund:
                if (ThrottleFirstUtil.isFastClick()) {
                    return;
                }
                if (orderDetialResultBean == null) {
                    getActivity().finish();
                    ToastUtils.showToast(getContext().getApplicationContext(), "订单详情异常！");
                    return;
                }

                mode.getTokens(getContext());
                mode.getPwdData().observe(this, s -> {
                    if (s) {
                        goBack(getActivity());
                        EventBus.getDefault().post(new MessageEvent(MessageEvent.TUIKUAN));
                    }
                });


                break;

        }
    }

    /**
     * 获取订单详情
     */
    private void getDataDetail() {
        mode.getLiveData().observe(this, bean -> {
            if (bean == null) {
                return;
            }
            orderDetialResultBean = bean;
            setUI();
        });
        mode.getOrderDetail(getContext(), getArguments().getString("orderNum"));
    }


    /**
     * 设置UI数据
     */
    private void setUI() {

        if (!StringUtils.isEmpty(orderDetialResultBean.getOrderSource())) {
            if ("1".equals(orderDetialResultBean.getOrderSource())) {
                llCertificationType.setVisibility(View.VISIBLE);
                llCarNumber.setVisibility(View.VISIBLE);
                if (StringUtils.isEmpty(orderDetialResultBean.getCertificationTypeName())) {
                    tvCertiftionTypeName.setText("暂无");
                } else {
                    tvCertiftionTypeName.setText(orderDetialResultBean.getCertificationTypeName());
                }

                if (StringUtils.isEmpty(orderDetialResultBean.getCarNumber())) {
                    tvCarNumber.setText("暂无");
                } else {
                    tvCarNumber.setText(orderDetialResultBean.getCarNumber());
                }
            } else {
                llCertificationType.setVisibility(View.GONE);
                llCarNumber.setVisibility(View.GONE);
            }
        }

        if (!StringUtils.isEmpty(orderDetialResultBean.getOrderSource())) {
            if ("3".equals(orderDetialResultBean.getOrderSource()) && !"0".equals(orderDetialResultBean.getOrderSource())) {
                tvOilsWenAn.setText("会员价");
            } else {
                tvOilsWenAn.setText("平台价");
            }


        }

        if (!StringUtils.isEmpty(orderDetialResultBean.getSize())) {//优惠券数据设置
            String couponStr;
            if (Integer.valueOf(orderDetialResultBean.getSize()) >= 1) {
                couponStr = "使用了" + orderDetialResultBean.getSize() + "张优惠券";
                tvCouponNum.setText(couponStr);
                if (!StringUtils.isEmpty(orderDetialResultBean.getCouponAmount())) {
                    String couponAmounts = orderDetialResultBean.getCouponAmount().substring(1, orderDetialResultBean.getCouponAmount().length());
                    tvAllCoupon.setText(couponAmounts);
                }
            } else {
                tvCouponNum.setText("当前无优惠券使用");
            }
        }


        tvOrderNums.setText(orderDetialResultBean.getOrderNumber());
        tvStationName.setText(orderDetialResultBean.getStoreName());
        tvOilsNums.setText(orderDetialResultBean.getOilNum() + "");
        tvYHAmount.setText("￥" + orderDetialResultBean.getActivityReductionDou());
        tvSFAmount.setText("￥" + orderDetialResultBean.getPayAmountDou());
        tvPayType.setText("" + orderDetialResultBean.getPayTypeStr());
        tvPayTime.setText("" + orderDetialResultBean.getTime());
        tvFPState.setText("" + orderDetialResultBean.getInvoiceStr());

        tvOilsPrice.setText("￥" + orderDetialResultBean.getOilsPrices());
        tvOilsRise.setText(orderDetialResultBean.getRiseNew() + "L");

        tvTremNum.setText(orderDetialResultBean.getTerminalNo());

        if (StringUtils.isEmpty(orderDetialResultBean.getPhone())) {
            llPhone.setVisibility(View.GONE);
        } else {
            llPhone.setVisibility(View.VISIBLE);
            tvMmeberPhone.setText(orderDetialResultBean.getPhone());
        }
        if (StringUtils.isEmpty(orderDetialResultBean.getRealName())) {
            llName.setVisibility(View.GONE);
        } else {
            llName.setVisibility(View.VISIBLE);
            tvMemberRealName.setText(orderDetialResultBean.getRealName());
        }


    }

    /**
     * 打印一键加油小票
     */
    private void printOneCareoe() {
        String title = "";
        if (StringUtils.isEmpty(orderDetialResultBean.getPayRule())) {
            title = "消费凭证(一键加油)";
        } else {
            if (orderDetialResultBean.getPayRule().equals("6"))
                title = "消费凭证(快速加油)";
            else
                title = "消费凭证(一键加油)";
        }

        PrintHelper.getInstance().getPrintOrder(title, orderDetialResultBean.getOrderAmountDou(), orderDetialResultBean.getPayAmountDou(), orderDetialResultBean.getStoreName(), Double.valueOf(orderDetialResultBean.getActivityReductionDou()),
                orderDetialResultBean.getOilNum(), orderDetialResultBean.getTime(), orderDetialResultBean.getOrderNumber(), orderDetialResultBean.getPhone(), orderDetialResultBean.getPayTypeStr(),
                Double.valueOf(orderDetialResultBean.getRiseNew()), Double.valueOf(orderDetialResultBean.getOilsPrices()), orderDetialResultBean.getOilPriceSwitch(), orderDetialResultBean.getCertificationTypeName(), orderDetialResultBean.getCarNumber(), true);
    }

    private void printYouKe() {
        PrintHelper.getInstance().PrintOrder(orderDetialResultBean.getOrderNumber(), orderDetialResultBean.getTime(), orderDetialResultBean.getOrderAmountDou(), orderDetialResultBean.getPayAmountDou(), orderDetialResultBean.getOilNum(), orderDetialResultBean.getStoreName(), orderDetialResultBean.getPayTypeStr(), Double.valueOf(orderDetialResultBean.getRiseNew()),
                Double.valueOf(orderDetialResultBean.getOilsPrices()), orderDetialResultBean.getOilPriceSwitch(), orderDetialResultBean.getTimeout(), true);
    }

    /**
     * 会员打印小票
     */
    private void print() {
        PrintHelper.getInstance().PrintMemberOrder(orderDetialResultBean.getOrderNumber(), orderDetialResultBean.getTime(), orderDetialResultBean.getOrderAmountDou(), orderDetialResultBean.getActivityReductionDou(), orderDetialResultBean.getPayAmountDou(), orderDetialResultBean.getOilNum(), orderDetialResultBean.getStoreName(), orderDetialResultBean.getPayTypeStr(), orderDetialResultBean.getMemberBlanceAmountsDou(), orderDetialResultBean.getPayType(), orderDetialResultBean.getSize(), orderDetialResultBean.getRiseNew(), orderDetialResultBean.getOilsPrices(), orderDetialResultBean.getOilPriceSwitch(), orderDetialResultBean.getMemberRedPackedAmountDou(), true, orderDetialResultBean.getMemberInfoCardDTO(), orderDetialResultBean.getOilNum(), orderDetialResultBean.getPhone(), orderDetialResultBean.getRedPackedAmount(), orderDetialResultBean.getStoreDownAmount());
    }

    private void showDialog() {
        mode.getdialogData().observe(this, aBoolean -> {
            if (aBoolean) {
                HUD.getInstance().show(getContext(), "正在退款中....");
            } else {
                HUD.getInstance().dismiss();
            }
        });
    }
}
